CMAKE_MINIMUM_REQUIRED(VERSION 3.8)

set(WITH_GPU @WITH_GPU@)
set(ENABLE_ORT_BACKEND @ENABLE_ORT_BACKEND@)
set(ENABLE_RKNPU2_BACKEND @ENABLE_RKNPU2_BACKEND@)
set(ENABLE_LITE_BACKEND @ENABLE_LITE_BACKEND@)
set(ENABLE_PADDLE_BACKEND @ENABLE_PADDLE_BACKEND@)
set(ENABLE_OPENVINO_BACKEND @ENABLE_OPENVINO_BACKEND@)
set(ENABLE_POROS_BACKEND @ENABLE_POROS_BACKEND@)
set(POROS_VERSION @POROS_VERSION@)
set(ENABLE_TRT_BACKEND @ENABLE_TRT_BACKEND@)
set(ENABLE_PADDLE_FRONTEND @ENABLE_PADDLE_FRONTEND@)
set(ENABLE_VISION @ENABLE_VISION@)
set(ENABLE_FLYCV @ENABLE_FLYCV@)
set(ENABLE_TEXT @ENABLE_TEXT@)
set(BUILD_ON_JETSON @BUILD_ON_JETSON@)
set(PADDLEINFERENCE_VERSION @PADDLEINFERENCE_VERSION@)
set(OPENVINO_VERSION @OPENVINO_VERSION@)
set(WITH_LITE_STATIC @WITH_LITE_STATIC@)
set(WITH_OPENCV_STATIC @WITH_OPENCV_STATIC@)
set(WITH_LITE_FULL_API @WITH_LITE_FULL_API@)
set(WITH_LITE_FP16 @WITH_LITE_FP16@)
# set(ENABLE_OPENCV_CUDA @ENABLE_OPENCV_CUDA@)
set(OPENCV_FILENAME @OPENCV_FILENAME@)
set(OPENVINO_FILENAME @OPENVINO_FILENAME@)
set(PADDLELITE_FILENAME @PADDLELITE_FILENAME@)
set(LIBRARY_NAME @LIBRARY_NAME@)
set(OPENCV_DIRECTORY "@OPENCV_DIRECTORY@")
set(ORT_DIRECTORY "@ORT_DIRECTORY@")
set(OPENVINO_DIRECTORY "@OPENVINO_DIRECTORY@")
set(RKNN2_TARGET_SOC "@RKNN2_TARGET_SOC@")

set(FASTDEPLOY_LIBS "")
set(FASTDEPLOY_INCS "")
list(APPEND FASTDEPLOY_INCS ${CMAKE_CURRENT_LIST_DIR}/include)

# Note(zhoushunjie): include some useful utils function
include(${CMAKE_CURRENT_LIST_DIR}/utils.cmake)

if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 11)
endif()

if(ANDROID)
  add_library(fastdeploy STATIC IMPORTED GLOBAL)
  set_property(TARGET fastdeploy PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_LIST_DIR}/lib/${ANDROID_ABI}/lib${LIBRARY_NAME}.so)
  list(APPEND FASTDEPLOY_LIBS fastdeploy)
else()
  find_library(FDLIB ${LIBRARY_NAME} ${CMAKE_CURRENT_LIST_DIR}/lib NO_DEFAULT_PATH)
  list(APPEND FASTDEPLOY_LIBS ${FDLIB})
endif()

if(ENABLE_ORT_BACKEND)
  if (ORT_DIRECTORY)
    set(ORT_LIB_PATH ${ORT_DIRECTORY}/lib)
  else()
    set(ORT_LIB_PATH ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/onnxruntime/lib)
  endif()
  message(STATUS "The path of ONNXRuntime is ${ORT_LIB_PATH}.")
  find_library(ORT_LIB onnxruntime ${ORT_LIB_PATH}  NO_DEFAULT_PATH)
  list(APPEND FASTDEPLOY_LIBS ${ORT_LIB})
endif()

if(ENABLE_PADDLE_BACKEND)
  find_library(PADDLE_LIB paddle_inference ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle_inference/paddle/lib NO_DEFAULT_PATH)
  if(WIN32)
    set(DNNL_LIB "${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle_inference/third_party/install/mkldnn/lib/mkldnn.lib")
    set(IOMP_LIB "${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle_inference/third_party/install/mklml/lib/libiomp5md.lib")
  elseif(APPLE)
    message(STATUS "No third parties libs(mkldnn and omp) need to link into paddle_inference on MacOS OSX.")
  else()
    set(DNNL_LIB "${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle_inference/third_party/install/mkldnn/lib/libmkldnn.so.0")
    set(IOMP_LIB "${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle_inference/third_party/install/mklml/lib/libiomp5.so")
  endif()
  if(NOT APPLE)
    list(APPEND FASTDEPLOY_LIBS ${PADDLE_LIB} ${DNNL_LIB} ${IOMP_LIB})
  else()
    list(APPEND FASTDEPLOY_LIBS ${PADDLE_LIB})
  endif()
endif()

if(ENABLE_OPENVINO_BACKEND)
  if (OPENVINO_DIRECTORY)
    set(OPENVINO_DIR ${OPENVINO_DIRECTORY})
  else()
    set(OPENVINO_DIR ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/${OPENVINO_FILENAME}/runtime)
  endif()
  get_openvino_libs(${OPENVINO_DIR})
  message(STATUS "OPENVINO_LIBS = ${OPENVINO_LIBS}")
  list(APPEND FASTDEPLOY_LIBS ${OPENVINO_LIBS})
endif()

if(ENABLE_RKNPU2_BACKEND)
  if(RKNN2_TARGET_SOC STREQUAL "RK356X")
    set(RKNPU2_LIB ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/rknpu2_runtime/RK356X/lib/librknn_api.so)
  elseif (RKNN2_TARGET_SOC STREQUAL "RK3588")
    set(RKNPU2_LIB ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/rknpu2_runtime/RK3588/lib/librknn_api.so)
  else ()
    message(FATAL_ERROR "RKNN2_TARGET_SOC is not set, ref value: RK356X or RK3588")
  endif()
  message(STATUS "The path of RKNPU2 is ${RKNPU2_LIB}.")
  list(APPEND FASTDEPLOY_LIBS ${RKNPU2_LIB})
endif()

if(ENABLE_LITE_BACKEND)
  set(LITE_DIR ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/${PADDLELITE_FILENAME})
  if(ANDROID)
    if(NOT WITH_LITE_STATIC)
      if(WITH_LITE_FULL_API)
        add_library(paddle_full_api_shared STATIC IMPORTED GLOBAL)
        set_property(TARGET paddle_full_api_shared PROPERTY IMPORTED_LOCATION ${LITE_DIR}/lib/${ANDROID_ABI}/libpaddle_full_api_shared.so)
        list(APPEND FASTDEPLOY_LIBS paddle_full_api_shared)
      else()
        add_library(paddle_light_api_shared STATIC IMPORTED GLOBAL)
        set_property(TARGET paddle_light_api_shared PROPERTY IMPORTED_LOCATION ${LITE_DIR}/lib/${ANDROID_ABI}/libpaddle_light_api_shared.so)
        list(APPEND FASTDEPLOY_LIBS paddle_light_api_shared)
      endif()
    endif()
  else()
    # Linux/Mac/Win/...
    find_library(LITE_LIB paddle_full_api_shared ${LITE_DIR}/lib NO_DEFAULT_PATH)
    list(APPEND FASTDEPLOY_LIBS ${LITE_LIB})
  endif()
endif()

if(ENABLE_POROS_BACKEND)
  find_library(POROS_LIB poros ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/poros/lib NO_DEFAULT_PATH)
  list(APPEND FASTDEPLOY_LIBS ${POROS_LIB})
  list(APPEND FASTDEPLOY_INCS ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/poros/include)
endif()

if(WITH_GPU)
  if (NOT CUDA_DIRECTORY)
    set(CUDA_DIRECTORY "/usr/local/cuda")
  endif()
  if(WIN32)
    find_library(CUDA_LIB cudart ${CUDA_DIRECTORY}/lib/x64)
  else()
    find_library(CUDA_LIB cudart ${CUDA_DIRECTORY}/lib64)
  endif()
  if(NOT CUDA_LIB)
    message(FATAL_ERROR "[FastDeploy] Cannot find library cudart in ${CUDA_DIRECTORY}, Please define CUDA_DIRECTORY, e.g -DCUDA_DIRECTORY=/path/to/cuda")
  endif()
  list(APPEND FASTDEPLOY_LIBS ${CUDA_LIB})

  if (ENABLE_TRT_BACKEND)
    if(BUILD_ON_JETSON)
      find_library(TRT_INFER_LIB nvinfer /usr/include/aarch64-linux-gnu/)
      find_library(TRT_ONNX_LIB nvonnxparser /usr/include/aarch64-linux-gnu/)
      find_library(TRT_PLUGIN_LIB nvinfer_plugin /usr/include/aarch64-linux-gnu/)
    else()
      find_library(TRT_INFER_LIB nvinfer ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tensorrt/lib NO_DEFAULT_PATH)
      find_library(TRT_ONNX_LIB nvonnxparser ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tensorrt/lib NO_DEFAULT_PATH)
      find_library(TRT_PLUGIN_LIB nvinfer_plugin ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tensorrt/lib NO_DEFAULT_PATH)
    endif()
    list(APPEND FASTDEPLOY_LIBS ${TRT_INFER_LIB} ${TRT_ONNX_LIB} ${TRT_PLUGIN_LIB})
  endif()
endif()

if(ENABLE_VISION)
  if(OPENCV_DIRECTORY)
    set(OpenCV_DIR ${OPENCV_DIRECTORY})
  else()
    if(ANDROID)
      set(OpenCV_DIR ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/${OPENCV_FILENAME}/sdk/native/jni)
    else()
      set(OpenCV_DIR ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/${OPENCV_FILENAME})
      if(WIN32)
        set(OpenCV_DIR ${OpenCV_DIR}/build)
      endif()
    endif()
  endif()
  message(STATUS "The path of OpenCV is ${OpenCV_DIR}.")
  if(ANDROID)
    if(WITH_OPENCV_STATIC)
      find_package(OpenCV REQUIRED PATHS ${OpenCV_DIR})
      list(APPEND FASTDEPLOY_INCS ${OpenCV_INCLUDE_DIRS})
      list(APPEND FASTDEPLOY_LIBS ${OpenCV_LIBS})
    else()
      set(OpenCV_INCLUDE_DIRS ${OpenCV_DIR}/include)
      get_filename_component(OpenCV_NATIVE_DIR ${OpenCV_DIR} DIRECTORY)
      set(OpenCV_LIBS_DIR ${OpenCV_NATIVE_DIR}/libs)
      if(ANDROID_TOOLCHAIN MATCHES "clang")  # use opencv 4.x
        add_library(opencv_java4 STATIC IMPORTED GLOBAL)
        set_property(TARGET opencv_java4 PROPERTY IMPORTED_LOCATION ${OpenCV_LIBS_DIR}/${ANDROID_ABI}/libopencv_java4.so)
        list(APPEND FASTDEPLOY_LIBS opencv_java4)
      elseif(ANDROID_TOOLCHAIN MATCHES "gcc")  # use opencv 3.x
        add_library(opencv_java3 STATIC IMPORTED GLOBAL)
        set_property(TARGET opencv_java3 PROPERTY IMPORTED_LOCATION ${OpenCV_LIBS_DIR}/${ANDROID_ABI}/opencv_java3.so)
        list(APPEND FASTDEPLOY_LIBS opencv_java3)
      else()
        message(FATAL_ERROR "Only support clang/gcc toolchain, but found ${ANDROID_TOOLCHAIN}.")
      endif()
      list(APPEND FASTDEPLOY_INCS ${OpenCV_INCLUDE_DIRS})
      message(STATUS "FASTDEPLOY_INCS: ${FASTDEPLOY_INCS}")
    endif()
  # Win/Linux/Mac
  else()
    find_package(OpenCV REQUIRED PATHS ${OpenCV_DIR} NO_DEFAULT_PATH)
    list(APPEND FASTDEPLOY_INCS ${OpenCV_INCLUDE_DIRS})
    list(APPEND FASTDEPLOY_LIBS ${OpenCV_LIBS})
  endif()

  if(ENABLE_FLYCV)
    include_directories(${CMAKE_CURRENT_LIST_DIR}/third_libs/install/flycv/include)
    set(FLYCV_LIB_DIR ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/flycv/lib)
    if(ANDROID)
      add_library(flycv_shared STATIC IMPORTED GLOBAL)
      set_property(TARGET flycv_shared PROPERTY IMPORTED_LOCATION ${FLYCV_LIB_DIR}/${ANDROID_ABI}/libflycv_shared.so)
      list(APPEND FASTDEPLOY_LIBS flycv_shared)
    else()
      find_library(FLYCV_LIB flycv_shared ${FLYCV_LIB_DIR} NO_DEFAULT_PATH)
      list(APPEND FASTDEPLOY_LIBS ${FLYCV_LIB})
    endif()
  endif()

endif()

if (ENABLE_TEXT)
  if(ANDROID)
    message(FATAL_ERROR "Not support fastdeploy text APIs with Android now!")
  endif()
  # Add dependency libs later
  find_library(FASTER_TOKENIZER_LIB core_tokenizers ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/faster_tokenizer/lib NO_DEFAULT_PATH)
  list(APPEND FASTDEPLOY_LIBS ${FASTER_TOKENIZER_LIB})
  list(APPEND FASTDEPLOY_INCS ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/faster_tokenizer/include)
  list(APPEND FASTDEPLOY_INCS ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/faster_tokenizer/third_party/include)
endif()

if(ENABLE_PADDLE_FRONTEND)
  if(ANDROID)
    message(FATAL_ERROR "Not support fastdeploy-paddle2onnx APIs with Android now!")
  endif()
  find_library(PADDLE2ONNX_LIB paddle2onnx  ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle2onnx/lib NO_DEFAULT_PATH)
  list(APPEND FASTDEPLOY_LIBS ${PADDLE2ONNX_LIB})
endif()

remove_duplicate_libraries(FASTDEPLOY_LIBS)

# Print compiler information
message(STATUS "")
message(STATUS "*************FastDeploy Building Summary**********")
message(STATUS "  CMake version             : ${CMAKE_VERSION}")
message(STATUS "  CMake command             : ${CMAKE_COMMAND}")
message(STATUS "  System                    : ${CMAKE_SYSTEM_NAME}")
message(STATUS "  C++ compiler              : ${CMAKE_CXX_COMPILER}")
message(STATUS "  C++ compiler version      : ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "  CXX flags                 : ${CMAKE_CXX_FLAGS}")
message(STATUS "  WITH_GPU                  : ${WITH_GPU}")
message(STATUS "  ENABLE_ORT_BACKEND        : ${ENABLE_ORT_BACKEND}")
message(STATUS "  ENABLE_RKNPU2_BACKEND     : ${ENABLE_RKNPU2_BACKEND}")
message(STATUS "  ENABLE_PADDLE_BACKEND     : ${ENABLE_PADDLE_BACKEND}")
message(STATUS "  ENABLE_POROS_BACKEND      : ${ENABLE_POROS_BACKEND}")
message(STATUS "  ENABLE_OPENVINO_BACKEND   : ${ENABLE_OPENVINO_BACKEND}")
message(STATUS "  ENABLE_TRT_BACKEND        : ${ENABLE_TRT_BACKEND}")
message(STATUS "  ENABLE_LITE_BACKEND       : ${ENABLE_LITE_BACKEND}")
if(ENABLE_PADDLE_BACKEND)
  message(STATUS "  Paddle Inference version  : ${PADDLEINFERENCE_VERSION}")
endif()
if(ENABLE_POROS_BACKEND)
  message(STATUS "  Poros version  : ${POROS_VERSION}")
endif()
if(ENABLE_OPENVINO_BACKEND)
  message(STATUS "  OpenVINO version          : ${OPENVINO_VERSION}")
endif()
message(STATUS "  ENABLE_TRT_BACKEND        : ${ENABLE_TRT_BACKEND}")
message(STATUS "  ENABLE_VISION             : ${ENABLE_VISION}")
message(STATUS "  ENABLE_TEXT               : ${ENABLE_TEXT}")
if(WITH_GPU)
  message(STATUS "  CUDA_DIRECTORY            : ${CUDA_DIRECTORY}")
endif()
if(OPENCV_DIRECTORY)
  message(STATUS "  OPENCV_DIRECTORY          : ${OPENCV_DIRECTORY}")
endif()
if(ORT_DIRECTORY)
  message(STATUS "  ORT_DIRECTORY             : ${ORT_DIRECTORY}")
endif()
if (OPENVINO_DIRECTORY)
  message(STATUS "  OPENVINO_DIRECTORY        : ${OPENVINO_DIRECTORY}")
endif()
if(ANDROID)
  message(STATUS "  ANDROID_ABI               : ${ANDROID_ABI}")
  message(STATUS "  ANDROID_PLATFORM          : ${ANDROID_PLATFORM}")
  message(STATUS "  ANDROID_NDK               : ${ANDROID_NDK}")
  message(STATUS "  WITH_OPENCV_STATIC:       : ${WITH_OPENCV_STATIC}")
  if(ENABLE_LITE_BACKEND)
    message(STATUS "  WITH_LITE_STATIC          : ${WITH_LITE_STATIC}")
    message(STATUS "  WITH_LITE_FULL_API        : ${WITH_LITE_FULL_API}")
    message(STATUS "  WITH_LITE_FP16            : ${WITH_LITE_FP16}")
  endif()
endif()
message(STATUS "  DEPENDENCY_LIBS           : ${FASTDEPLOY_LIBS}")

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.4.0")
    string(STRIP "${CMAKE_CXX_COMPILER_VERSION}" CMAKE_CXX_COMPILER_VERSION)
    message(FATAL_ERROR "[ERROR] FastDeploy require g++ version >= 5.4.0, but now your g++ version is ${CMAKE_CXX_COMPILER_VERSION}, this may cause failure! Use -DCMAKE_CXX_COMPILER to define path of your compiler.")
  endif()
endif()

function(install_fastdeploy_libraries DESTINATION_DIR)
  set(DYN_LIB_SUFFIX "*.so*")
  if(WIN32)
    set(DYN_LIB_SUFFIX "*.dll")
  elseif(APPLE)
    set(DYN_LIB_SUFFIX "*.dylib*")
  endif()
  if(FastDeploy_DIR)
    set(DYN_SEARCH_DIR ${FastDeploy_DIR})
  elseif(FASTDEPLOY_INSTALL_DIR)
    set(DYN_SEARCH_DIR ${FASTDEPLOY_INSTALL_DIR})
  else()
    message(FATAL_ERROR "Please set FastDeploy_DIR/FASTDEPLOY_INSTALL_DIR before call install_fastdeploy_libraries.")
  endif()
  file(GLOB_RECURSE ALL_NEED_DYN_LIBS ${DYN_SEARCH_DIR}/${DYN_LIB_SUFFIX})
  if(ENABLE_VISION)
    file(GLOB_RECURSE ALL_OPENCV_DYN_LIBS ${OpenCV_DIR}/${DYN_LIB_SUFFIX})
    list(REMOVE_ITEM ALL_NEED_DYN_LIBS ${ALL_OPENCV_DYN_LIBS})
    # Only Install the necessary libraries
    if(NOT WITH_OPENCV_STATIC)
      if(WIN32)
        file(GLOB OPENCV_DYN_LIBS ${OpenCV_DIR}/x64/vc15/bin/${DYN_LIB_SUFFIX})
      elseif(ANDROID)
        file(GLOB OPENCV_DYN_LIBS ${OpenCV_DIR}/libs/${ANDROID_ABI}/${DYN_LIB_SUFFIX})
      else() # linux/mac
        file(GLOB OPENCV_DYN_LIBS ${OpenCV_DIR}/lib/${DYN_LIB_SUFFIX})
      endif()
      file(INSTALL ${OPENCV_DYN_LIBS} DESTINATION ${DESTINATION_DIR})
    endif()
  endif()
  if(ENABLE_OPENVINO_BACKEND)
    # need plugins.xml for openvino backend
    set(OPENVINO_RUNTIME_BIN_DIR ${OPENVINO_DIR}/bin)
    file(GLOB OPENVINO_PLUGIN_XML ${OPENVINO_RUNTIME_BIN_DIR}/*.xml)
    file(INSTALL ${OPENVINO_PLUGIN_XML} DESTINATION ${DESTINATION_DIR})
  endif()
  # Install other libraries
  file(INSTALL ${ALL_NEED_DYN_LIBS} DESTINATION ${DESTINATION_DIR})
endfunction()
